# This makefile controls the build of worthyd.

svn_revision := $(shell svn info | grep "Revision" | cut -f 2 -d ' ')
# svn_branch is an 'a' if this is a branch, rather than trunk
svn_branch := $(shell if svn info | grep '^URL' | grep -q 'Appliansys/branches' ; then echo 'a' ; fi)
svn_info := $(shell svn info > svn.info 2>&1)
version := 0.1.$(svn_revision)$(svn_branch)

all: worthyd-$(version)_slackware7.0.tgz dist/worthyd-$(version).tar.gz worthyd-$(version).deb
test: worthyd/*.py
	cd worthyd && ./nodes.py
	cd worthyd && ./udp.py
	cd worthyd && ./command_timeout.py
	find worthyd/ -name "*.pyc" -exec rm {} \;

clean:
	rm -f *.tgz
	rm -f *.gz
	rm -f *.deb
	rm -rf dist
	rm -rf pkg

# Make package for DNSBox100
slackware_site_packages := /usr/lib/python/site-packages/
worthyd-$(version)_slackware7.0.tgz: worthyd/*.py
	tar -cvf worthyd_slackware7.0.tar worthyd/*.py --transform 's|^worthyd/|$(slackware_site_packages)|'
	tar -rvf worthyd_slackware7.0.tar etc/worthyd/worthyd.conf --transform 's|^etc/|/etc/|'
	ln -s $(slackware_site_packages)worthyd/main.py main.py
	tar -rvf worthyd_slackware7.0.tar main.py --transform 's|^main.py|/usr/sbin/worthyd|'
	rm main.py
	tar -rvf worthyd_slackware7.0.tar svn.info --transform 's|^svn.info|/tmp/svn.info|'
	rm svn.info
	gzip worthyd_slackware7.0.tar
	mv worthyd_slackware7.0.tar.gz worthyd-$(version)_slackware7.0.tgz

# Make distutils package
dist/worthyd-$(version).tar.gz: worthyd/*.py setup.py
	./setup.py sdist

# Make debian package
python_support := usr/share/python-support/
worthyd-$(version).deb: worthyd/*.py etc/worthyd/
	rm -rf pkg
	mkdir -p pkg/$(python_support)
	cp -a worthyd pkg/$(python_support)
	cp -a etc pkg/
	mkdir pkg/DEBIAN
	cat DEBIAN/control | sed 's/Version:/Version: $(version)/g' > pkg/DEBIAN/control
	mkdir -p pkg/usr/sbin
	ln -s /$(python_support)worthyd/main.py pkg/usr/sbin/worthyd
	chmod -R a+rx pkg/DEBIAN 
	chmod -R g+rx pkg/DEBIAN 
	dpkg --build pkg/ worthyd-$(version)_all.deb

